package org.ucas.faker.web.annotation;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.ucas.faker.web.entity.Depot;
import org.ucas.faker.web.entity.patents.*;
import org.ucas.faker.web.entity.User;
import org.ucas.faker.web.fetcher.FetchReqBuilder;
import org.ucas.faker.web.fetcher.cond.CompCond;
import org.ucas.faker.web.fetcher.cond.FetchCond;
import org.ucas.faker.web.fetcher.meta.ConstantField;
import org.ucas.faker.web.fetcher.meta.ConstraintField;
import org.ucas.faker.web.fetcher.parse.TableMeta;
import org.ucas.faker.web.fetcher.parse.TableParser;
import org.ucas.faker.web.service.PatentService;
import org.ucas.faker.web.utils.model.Coordinator;
import org.ucas.faker.web.service.ApplicationService;
import org.ucas.faker.web.service.DepotService;
import org.ucas.faker.web.service.UserService;
import org.ucas.faker.web.utils.wx.WxClient;

/**
 * Created by suemi on 17-7-7.
 */
@Component
public class RegistryDumper implements InitializingBean{

  @Autowired
  private ApplicationService applicationService;

  @Autowired
  private UserService userService;

  @Autowired
  private DepotService depotService;

  @Autowired
  private PatentService patentService;

  @Autowired
  private WxClient client;

  private void parseTables() {
    TableParser parser = new TableParser(FetchReqBuilder.meta);
    parser.parse(PatentMeta.class);
    parser.parse(Patent.class);
    parser.parse(PatentFeature.class);
    parser.parse(PatentContent.class);
    parser.parse(Symbol.class);
    parser.parse(Agency.class);
    parser.parse(PatentAgency.class);
    parser.buildGraph();
  }

  private void supplyFields() {

    TableMeta m = FetchReqBuilder.meta;

    FetchCond cond= new CompCond(m.column("agency.kind"),"=",new ConstantField(Agency.Category.APPLICANT));
    m.add("ap",m.column("agency.name"),cond);

    cond= new CompCond(m.column("agency.kind"),"=",new ConstantField(Agency.Category.INVENTOR));
    m.add("inventor",m.column("agency.name"),cond);

    cond= new CompCond(m.column("agency.kind"),"=",new ConstantField(Agency.Category.ASSIGNEE));
    m.add("pa",m.column("agency.name"),cond);

    cond = new CompCond(m.column("symbol.kind"),"=",new ConstantField(Symbol.Category.IPC));
    m.add("ic",m.column("symbol.original"),cond);

    cond = new CompCond(m.column("symbol.kind"),"=",new ConstantField(Symbol.Category.IPC_MAIN));
    m.add("icm",m.column("symbol.original"),cond);

    cond = new CompCond(m.column("symbol.kind"),"=",new ConstantField(Symbol.Category.CPC));
    m.add("cpc",m.column("symbol.original"),cond);

//    cond = new CompCond(m.column("symbol.kind"),"=",new ConstantField(Symbol.Category.UPC));
//    m.add(new ConstraintField("upcs",m.column("symbol.original"),cond));

    cond = new CompCond(m.column("symbol.kind"),"=", new ConstantField(Symbol.Category.BRANCH));
    m.add("br",m.column("symbol.original"),cond);

    cond = new CompCond(m.column("symbol.kind"),"=", new ConstantField(Symbol.Category.EFFECT));
    m.add("ef",m.column("symbol.original"),cond);
    
    cond = new CompCond(m.column("agency.kind"),"=", new ConstantField(Agency.Category.APPLICANT));
    m.add("prc",m.column("agency.country"),cond);
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    applicationService.clear();
    applicationService.persist(RegistryScanner.applications);

    User root = userService.findByAccount("root");
    if(root == null) {
      root = new User();
      root.setAccount("root");
      root.setPassword("123456");
      root.setId(userService.insert(root));
    }

    Depot depot = depotService.findByName(root.getId(),"main");
    if(depot == null) {
      depot = depotService.create(root.getId(),"main");
    }
    Coordinator.mainDepotId = depot.getId();

    PatentWrapper.service = patentService;

    parseTables();
    supplyFields();

//    client.authorize();
  }
}
